package com.academico.web.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseServlet extends HttpServlet {

	private static final long serialVersionUID = -2763873786323729523L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		this.processRequest(request, response);
	}

	public void setAttributeToRequest(HttpServletRequest request, String key,
			Object obj) {
		request.setAttribute(key, obj);
	}

	public void forward(String page, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher(page);
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void redirect(String page, HttpServletResponse response)
			throws IOException {
		String urlWithSessionID = response.encodeRedirectURL(page);
		response.sendRedirect(urlWithSessionID);
	}

	public abstract void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException;

}
